<template>
  <div class="w">

    <!-- 表单区 -->
    <Form style="padding-top: 35px;" 
      name="form" 
      ref="formRef" 
      :labelCol="{ span: 6 }" 
      :wrapperCol="{ span: 18 }" 
      :initialValues="form"
      @finish="formFinish">

      <Row>
        <Col :span="20" :offset="0">
          <FormItem name="dictType" label="字典类型" :rules="formRules.username">
            <Input disabled  style="width: 100%"/>
          </FormItem>
        </Col>
      </Row>
      <Row>
        <Col :span="20" :offset="0">
          <FormItem name="dictLabel" label="数据标签" :rules="formRules.dictNameRules">
            <Input placeholder="请输入数据标签" style="width: 100%" />
          </FormItem>
        </Col>
      </Row>
      <Row>
        <Col :span="20" :offset="0">
         <FormItem name="dictValue" label="数据键值" :rules="formRules.dictValueRules">
            <Input placeholder="请输入数据键值" style="width: 100%" />
          </FormItem>
        </Col>
      </Row>
      <Row>
        <Col :span="20" :offset="0">
         <FormItem
              name="dictSort"
              label="排序"
              initialValue="0"
            >
              <InputNumber :min="0" style="width: 100%" />
            </FormItem>
        </Col>
      </Row>
       <Row>
        <Col :span="20" :offset="0">
       <FormItem name="status" label="状态">
            <RadioGroup
              :options="dict.type.sys_normal_disable"
              name="status"
            />
          </FormItem>
        </Col>
      </Row>
       <Row>
        <Col :span="20" :offset="0">
     <FormItem name="remark" label="备注">
            <TextArea placeholder="请输入" style="width: 100%" allowClear />
          </FormItem>
        </Col>
      </Row>
      <Row style="margin-top: 20px;">
        <Col :span="12" :offset="6">
          <FormItem :wrapperCol="{ span: 24 }" style="text-align: center">
            <Button style="width: 100px;" type="primary" @click="formValidate">提 交</Button>
            <Button style="width: 100px; margin-left: 20px;" @click="formCancel">取 消</Button>
          </FormItem>
        </Col>
      </Row>
    </Form>
  </div>
</template>

<script>
import * as ruoyi from '@/utils/ruoyi'
import { listData, getData, delData, addData, updateData } from "@/api/system/dict/data";
import { Message } from "@uni/vue";
export default {
  name: "app",
    dicts: ["sys_normal_disable"],
  props: {
    id: {
      type: Number,
      default: 0
    },
    currRow: {
      default: () => {}
    },
    refData: {
      default: () => {}
    }
  },
  data() {
    return {
      form: {
        dictName: this.currRow.dictName,
        dictLabel: this.currRow.dictLabel,
        status: this.currRow.status?this.currRow.status:'0',
        dictValue: this.currRow.dictValue,
        dictSort: this.currRow.dictSort,
        dictId: this.currRow.dictId,
        remark: this.currRow.remark,
        dictType: this.currRow.dictType,
      },
      formRules: {
        dictNameRules: [{ required: true, message: "数据标签不能为空" }],
      dictValueRules: [{ required: true, message: "数据键值不能为空" }],
      },
      roleOptions: [],
      isModalVisible: false
      
    }
  },
  mounted() {

  },
  methods: {
    formValidate: function () {
      this.$refs.formRef.submit();
    },
    formCancel: function () {
      // this.$refs.formRef.reset();
      this.handleCancel();
    },
    formFinish: function (formValues) {
      this.form = formValues;
      this.formSubmit()
    },
    async formSubmit() {
      // 公共属性
      this.form.createBy = this.$store.state.user.name;
      this.form.createTime = ruoyi.parseTime(Date.now());
      this.form.updateBy = this.$store.state.user.name;
      this.form.updateTime = ruoyi.parseTime(Date.now());

      // 提交表单请求
      this.isModalVisible = false;
      let reqForm = JSON.parse(JSON.stringify(this.form));
      // console.log("reqForm: ", reqForm);
      reqForm.status = reqForm.status === "0" ? "0" : "1";
      if (this.id) {
        // 修改
        reqForm.dictCode = this.id;
        const rs = await updateData(reqForm);
        if (rs.code !== 200) return Message.error({ text: '修改信息失败：' + rs.msg });
        Message.success({ text: '修改信息完成' });
        this.handleCancel();

      } else {
        // 新增
        const rs = await addData(reqForm);
        if (rs.code !== 200) return Message.error({ text: '新增信息失败：' + rs.msg });
        Message.success({ text: '新增信息完成' });
        this.handleCancel();
      }
    },
    handleCancel: function () {
      this.$emit('handleCancel');
    },
    selectOnChange(val) {
      this.modalValue = val;
    },

  }
}
</script>

<style scoped>
  .w {
    background-color: #F9FAFB;
  }

  /* 标题 */
  .title-area span {
    display: inline-block;
    font-weight: 500;
    line-height: 36px;
    margin-left: 15px;
  }
</style>
